package com.teamone.mobilewatch.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * 
 * Holds user setting values
 * 
 */
public class UserSettings {

	private static final String TAG = "UserSettings";

	private static String webServiceUri[] ={ "http://10.0.2.2:62084/PatientWatchWebService/DataService.asmx",
					"http://10.0.2.2:49461/PatientWatchWebServiceReplication/DataService.asmx"};
	private static int currentWebServiceUriIndex = 0; 
	
	private static int bphNorm, bplNorm, hrNorm, thresholdUnstable,
			thresholdCritical, refreshRate, historyPeriod;
	
	private static float tNorm;
	private static boolean voiceAlerts = false;
	// true if user settings loaded from shared preferences
	private static boolean loaded = false;


	/**
	 * Gets history period
	 * @return
	 */
	public static int getHistoryPeriod() {
		return historyPeriod;
	}

	/**
	 * Sets history period
	 * @param historyPeriod
	 */
	public static void setHistoryPeriod(int historyPeriod) {
		UserSettings.historyPeriod = historyPeriod;
	}

	/**
	 * Gets blood pressure H norm
	 */
	public static int getBphNorm() {
		return bphNorm;
	}

	/**
	 * Sets blood pressure H norm
	 * 
	 * @param bphNorm
	 *            - blood pressure value
	 */
	public static void setBphNorm(int bphNorm) {
		UserSettings.bphNorm = bphNorm;
	}

	/**
	 * Gets blood pressure L norm
	 */
	public static int getBplNorm() {
		return bplNorm;
	}

	/**
	 * Sets blood pressure L norm
	 * 
	 * @param bplNorm
	 *            - blood pressure value
	 */
	public static void setBplNorm(int bplNorm) {
		UserSettings.bplNorm = bplNorm;
	}

	/**
	 * Gets heart rate norm
	 */
	public static int getHrNorm() {
		return hrNorm;
	}

	/**
	 * Sets heart rate norm
	 * 
	 * @param hrNorm
	 *            - heart rate value
	 */
	public static void setHrNorm(int hrNorm) {
		UserSettings.hrNorm = hrNorm;
	}

	/**
	 * Gets body temperature norm
	 */
	public static float getTNorm() {
		return tNorm;
	}

	/**
	 * Gets body temperature norm
	 * 
	 * @param tNorm
	 *            - value of temperature
	 */
	public static void setTNorm(float tNorm) {
		UserSettings.tNorm = tNorm;
	}

	/**
	 * Gets threshold for Unstable Condition
	 */
	public static int getThresholdUnstable() {
		return thresholdUnstable;
	}

	/**
	 * Sets threashold for Unstable Condition
	 * 
	 * @param threasholdUnstable
	 *            - value
	 */
	public static void setThresholdUnstable(int threasholdUnstable) {
		UserSettings.thresholdUnstable = threasholdUnstable;
	}

	/**
	 * Gets threashold for Critical Condition
	 */
	public static int getThresholdCritical() {
		return thresholdCritical;
	}

	/**
	 * Sets threashold for Critical Condition
	 * 
	 * @param thresholdUnstable
	 *            - value
	 */
	public static void setThresholdCritical(int threasholdCritical) {
		UserSettings.thresholdCritical = threasholdCritical;
	}

	/**
	 * Returns if Voice Alert flag is set
	 */
	public static boolean isVoiceAlerts() {
		return voiceAlerts;
	}

	/**
	 * Sets Voice Alert flag
	 * 
	 * @param voiceAlerts
	 *            - flag value
	 */
	public static void setVoiceAlerts(boolean voiceAlerts) {
		UserSettings.voiceAlerts = voiceAlerts;
	}

//	/**
//	 * Sets webservice URI string
//	 * 
//	 * @param webServiceUri
//	 */
//	public static void setWebServiceUri(String webServiceUri) {
//		UserSettings.webServiceUri = webServiceUri;
//	}

	/**
	 * Returns webservice URI string
	 */
	public static String getWebServiceUri() {
		return webServiceUri[currentWebServiceUriIndex];

	}
	
	/**
	 * Switch to the next replication webservice URI string
	 */
	public static void switchWebServiceUri() {	
		currentWebServiceUriIndex =(currentWebServiceUriIndex+1) % webServiceUri.length;
	}

	/**
	 * Gets refresh rate
	 * 
	 * @return
	 */
	public static int getRefreshRate() {
		return refreshRate;
	}

	/**
	 * Sets refresh rate
	 * 
	 * @param refreshRate
	 *            - refresh rate (webservice poll rate in seconds)
	 */
	public static void setRefreshRate(int refreshRate) {
		UserSettings.refreshRate = refreshRate;
	}

	/**
	 * Loads user settings from shared preference
	 * 
	 * @param context
	 *            - activity context
	 * @return <false> if unable to load settings from shared preferences
	 */
	public static boolean load(Context context) {
		boolean loadSuccess = false;
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);

		
		
		
		setThresholdCritical(Integer.parseInt(prefs.getString("critical_threshold", "-1")));
		setThresholdUnstable(Integer.parseInt(prefs.getString("unstable_threshold", "-1")));
		setBphNorm(Integer.parseInt(prefs.getString("bph_norm", "-1")));
		setBplNorm(Integer.parseInt(prefs.getString("bpl_norm", "-1")));
		setRefreshRate(Integer.parseInt(prefs.getString("refresh_rate", "-1")));

		setVoiceAlerts(prefs.getBoolean("voice_alerts", false));
		setHrNorm(Integer.parseInt(prefs.getString("hr_norm", "-1")));
		setHistoryPeriod(Integer.parseInt(prefs.getString("history_period", "-1")));
		setTNorm(Float.parseFloat(prefs.getString("t_norm", "-1")));
		
		if (thresholdCritical < 0 || thresholdUnstable < 0 || bphNorm < 0
				|| bplNorm < 0 || refreshRate < 0 || hrNorm < 0
				|| historyPeriod < 0 || tNorm < 0) {
			Log.d(TAG, "No Data found in SharedPreferences");
		} else {
			loadSuccess = true;
			Log.d(TAG, "Loaded from SharedPreferences: OK");
		}
//
//		Map<String, ?> map = prefs.getAll();
//		Set<String> keys = map.keySet();
//		for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
//			String key = (String) iterator.next();
//			Log.d(TAG, "SharedPreferences: key=" + key);
//		}

		loaded = loadSuccess;
		return loadSuccess;
	}
	
	/**
	 * Returns flag indicating if data is loaded
	 * @return
	 */
	public static boolean isLoaded() {
		return loaded;
	}

}
